package com.yuandm.wxnt;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;





public class SyncHttp
{

    public String httpGet(String url, String params) throws Exception
    {
        String response = null;

        if (null!=params&&!params.equals(""))
        {
            url += "?" + params;
        }

        int timeoutConnection = 3000;
        int timeoutSocket = 3000;
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);


        HttpClient httpClient = new DefaultHttpClient(httpParameters);



        HttpGet httpGet = new HttpGet(url);


        try
        {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();

            do{

                response = EntityUtils.toString(httpResponse.getEntity());
            }while (statusCode != HttpStatus.SC_OK); //SC_OK = 200

        } catch (Exception e)
        {
            throw new Exception(e);
        }
        return response;
    }


    public String httpPost(String url, List<Parameter> params) throws Exception
    {
        String response = null;
        int timeoutConnection = 3000;
        int timeoutSocket = 3000;
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        HttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(url);
        if (params.size()>=0)
        {

            httpPost.setEntity(new UrlEncodedFormEntity(buildNameValuePair(params),HTTP.UTF_8));
        }

        HttpResponse httpResponse = httpClient.execute(httpPost);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if(statusCode==HttpStatus.SC_OK)
        {

            response = EntityUtils.toString(httpResponse.getEntity());
        }
        else
        {
            response = "err"+statusCode;
        }
        return response;
    }


    private List<BasicNameValuePair> buildNameValuePair(List<Parameter> params)
    {
        List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
        for (Parameter param : params)
        {
            BasicNameValuePair pair = new BasicNameValuePair(param.getName(), param.getValue());
            result.add(pair);
        }
        return result;
    }
}
